package saucaca.Service;

import saucaca.Service.MusicService.MediaPlayerState;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

public class NotificationBroadcast extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
            if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
                return;
            Intent i = new Intent(context, MusicService.class);
            switch (keyEvent.getKeyCode()) {
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                	if(MusicService.mState==MediaPlayerState.Playing){
                		i.setAction(MusicService.ACTION_PAUSE);
    					context.startService(i);
                	}else{
                		i.setAction(MusicService.ACTION_PLAY);
    					context.startService(i);
                	}
                	break;
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                	break;
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                	break;
                case KeyEvent.KEYCODE_MEDIA_STOP:
                	break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                	Log.d("TAG", "TAG: KEYCODE_MEDIA_NEXT");
					i.setAction(MusicService.ACTION_NEXT);
					context.startService(i);
                	break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                	Log.d("TAG", "TAG: KEYCODE_MEDIA_PREVIOUS");
					i.setAction(MusicService.ACTION_PREVIOUS);
					context.startService(i);
                	break;
            }
		}  else{
			 Intent i = new Intent(context, MusicService.class);
            	if (intent.getAction().equals(MusicService.ACTION_PLAY)) {
            		i.setAction(MusicService.ACTION_PLAY);
					context.startService(i);
        		} else if (intent.getAction().equals(MusicService.ACTION_PAUSE)) {
        			i.setAction(MusicService.ACTION_PAUSE);
					context.startService(i);
        		} else if (intent.getAction().equals(MusicService.ACTION_NEXT)) {
        			i.setAction(MusicService.ACTION_NEXT);
					context.startService(i);
        		} else if (intent.getAction().equals(MusicService.ACTION_DELETE)) {
					Intent i2 = new Intent(context, MusicService.class);
					context.stopService(i2);
        		}else if (intent.getAction().equals(MusicService.ACTION_PREVIOUS)) {
        			i.setAction(MusicService.ACTION_PREVIOUS);
					context.startService(i);
        		}
		}
	}
	
	public String ComponentName() {
		return this.getClass().getName(); 
	}
}
